﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class customer_service_user_change_personal_details : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ((AfterLogin)this.Master).mainCategory = "SERVICE";

        if (IsPostBack == false)
        {

            if (Session["userId"] == null) return;

            this.tbFirstName.Text = Session["fName"] as String;
            this.tbLastName.Text = Session["lName"] as String;
            this.tbNRIC.Text = Session["custNRIC"] as String;
            this.tbNationality.Text = Session["custNationality"] as String;
            String gender = Session["custGender"] as String;
            if ("Female".Equals(gender))
            {
                this.rbFemale.Checked = true;
            }
            else
            {

            } this.rbMale.Checked = true;

            Object dob = Session["custDob"];
            if (dob != null)
            {

                this.tbDob.Text = ((DateTime)dob).ToString("dd/MM/yyyy");
            }

            this.tbPhone.Text = Session["custMobile"] as String;
            this.tbEmail.Text = Session["custEmail"] as String;
            this.tbAddress.Text = Session["custAddress"] as String;
            this.tbCode.Text = Session["custPostal"] as String;
            this.tbOccupation.Text = Session["custOccupation"] as String;
        }
    }
    protected void btnChange_Click(object sender, EventArgs e)
    {

        string fName = tbFirstName.Text;
        string lName = tbLastName.Text;
        string dof = tbDob.Text;
        string nationality = tbNationality.Text;
        string nric = tbNRIC.Text;
        string phone = tbPhone.Text;
        string email = tbEmail.Text;
        string add = tbAddress.Text;
        string postal = tbCode.Text;
        string occupation = tbOccupation.Text;

          string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection myConnection = new SqlConnection(strConnectionString);

        myConnection = DBManager.getSqlConnection();

        SqlCommand myCommand = new SqlCommand("UPDATE CUSTOMER SET fName = @fname, lName = lname, custDob =@DOB, custNationality=@nationality, custNRIC=@nric, custMobile=@phone, email=@email, custAddress=@add, custPostal=@postal, custOccupation=@occupation WHERE custID = @custID ", myConnection);


        myConnection.Open();

        try
        {
             
            myCommand.Parameters.AddWithValue("@fname", tbFirstName.Text);
            myCommand.Parameters.AddWithValue("@lName", tbLastName.Text);
            myCommand.Parameters.AddWithValue("@DOB", Util.StringToDate(tbDob.Text));
            myCommand.Parameters.AddWithValue("@nationality", tbNationality.Text);
            myCommand.Parameters.AddWithValue("@nric", tbNRIC.Text);
            myCommand.Parameters.AddWithValue("@phone", tbPhone.Text);
            myCommand.Parameters.AddWithValue("@email", tbEmail.Text);
            myCommand.Parameters.AddWithValue("@add", tbAddress.Text);
            myCommand.Parameters.AddWithValue("@postal", tbCode.Text);
            myCommand.Parameters.AddWithValue("@occupation", tbOccupation.Text);
            myCommand.Parameters.AddWithValue("@custID", Session["userId"]);
            myCommand.ExecuteNonQuery();
            myConnection.Close();

            Alert alert = new Alert();
            alert.addAlertPersonalParticulars((String)Session["userId"],"You have changed your personal particulars.","");

        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            Response.Write(ex);
        }
        finally
        {
            myConnection.Close();
        }



        String CustID = Session["userId"] as String;
        string sql = "select C.custID, C.fName,C.lName,S.accountNo,C.custGender,C.custNRIC,C.custNationality,C.custDob,C.custTel,C.custMobile,C.custAddress,C.email,C.custOccupation,C.custPostal,C.custMarital" +

                                            " From CUSTOMER C INNER JOIN SAVING_ACCOUNT S ON C.custID = S.custID Where C.custID='" + CustID + "'";
        
        SqlCommand cmd = new SqlCommand(sql, myConnection);
        myConnection.Open();
        SqlDataReader dr = cmd.ExecuteReader();

       
        if (dr.Read())
        {
            Session["userName"] = dr.GetString(1) + dr.GetString(2);
            Session["userId"] = dr.GetString(0);
            Session["fName"] = dr.GetString(1);
            Session["lName"] = dr.GetString(2);
            Session["savingAccountNo"] = dr.GetString(3);
            Session["custGender"] = dr.GetString(4);
            Session["custNRIC"] = dr.GetString(5);
            Session["custNationality"] = dr.GetString(6);
            Session["custDob"] = dr.GetDateTime(7);
            Session["custTel"] = dr.GetString(8);
            Session["custMobile"] = dr.GetString(9);
            Session["custAddress"] = dr.GetString(10);
            Session["custEmail"] = dr.GetString(11);
            Session["custOccupation"] = dr.GetString(12);
            Session["custPostal"] = dr.GetString(13);
            Session["custMarital"] = dr.GetString(14);  

        }
        dr.Close();
        
    }
}